//
// Created by ls on 23-12-20.
//
#include <iostream>
#include <string>
// #include <utility>
#include <vector>

// vector 容器中存放自定义的数据类型
class Person
{
public:
    Person(std::string name, const int age)
    {
        this->m_Name = std::move(name);
        this->m_Age  = age;
    }
    std::string m_Name;
    int m_Age;
};

void test01()
{
    std::vector<Person> v;
    const Person p1("孙悟空",10);
    const Person p2("猪八戒",9);
    const Person p3("沙悟净",22);
    const Person p4("唐三藏",3);
    const Person p5("白骨精",999);

    // 向容器中添加数据
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);

    // 遍历容器中的数据
    for (const auto& it:v)
    {
        std::cout << "姓名：" << it.m_Name << " 年龄：" << it.m_Age  << std::endl;
    }

    // 遍历容器中的数据  (老方法-舍弃)
    // for (std::vector<Person>::iterator it = v.begin(); it != v.end(); it++)
    // {
    //     std::cout << "姓名：" << (*it).m_Name << " 年龄：" << (*it).m_Age << std::endl;
    // }
}


void test02()
{
    std::vector<Person *> v;
    // 当传入指针至 push_back中时，不可以是 const 类型
    Person p1("孙悟空",10);
    Person p2("猪八戒",9);
    Person p3("沙悟净",22);
    Person p4("唐三藏",3);
    Person p5("白骨精",999);

    // 向容器中添加数据
    v.push_back(&p1);
    v.push_back(&p2);
    v.push_back(&p3);
    v.push_back(&p4);
    v.push_back(&p5);

    // 遍历容器中的数据
    for (const auto& it:v)
    {
        std::cout << "姓名：" << it->m_Name << " 年龄：" << it->m_Age  << std::endl;
    }

}

int main()
{

    test01();
    test02();
    return 0;
}
